<template>
  <div>
    <br>
    <el-form :inline="true">
      <el-form-item label="订单id">
        <el-input v-model="searchMap.id" placeholder="订单id"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-select v-model="searchMap.status" placeholder="请选择">
          <el-option
            v-for="item in statusList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-button type="primary" @click="fetchData()">查询</el-button>
    </el-form>
    <el-table
      :data="list"
      style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table
            :data="props.row.orderItems"
            style="width: 100%">
            <el-table-column
              label="商品信息">
              <template slot-scope="scope">
                <span><img class="lazy" :src="scope.row.book.picture" width="80px" height="100px"></span>
                <span>{{ scope.row.book.name }}</span>
              </template>
            </el-table-column>
            <el-table-column label="单价(元)" width="120">
              <template slot-scope="scope">
                {{ (scope.row.book.price * 0.01).toFixed(2) }}
              </template>
            </el-table-column>
            <el-table-column
              prop="number"
              label="数量"
              width="150">
            </el-table-column>
            <el-table-column label="金额(元)" width="120">
              <template slot-scope="scope">
                {{ (scope.row.book.price * scope.row.number * 0.01).toFixed(2) }}
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column
        label="订单号"
        prop="id">
      </el-table-column>
      <el-table-column
        label="备注"
        prop="userMessage">
      </el-table-column>
      <el-table-column
        label="订单总额">
        <template slot-scope="scope">
          {{ (totalPrice(scope.row.orderItems) * 0.01).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column
        label="收货地址"
        prop="address.address">
      </el-table-column>
      <el-table-column
        label="收货人"
        prop="address.receiveName">
      </el-table-column>
      <el-table-column
        label="联系方式"
        prop="address.phone">
      </el-table-column>
      <el-table-column
        label="下单时间"
        prop="createDate">
      </el-table-column>
      <el-table-column
        label="付款时间"
        prop="payDate">
      </el-table-column>
      <el-table-column
        label="发货时间"
        prop="deliveryDate">
      </el-table-column>
      <el-table-column
        label="确认收货时间"
        prop="confirmDate">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status === 2" @click="toSend(scope.row.id)" type="success" size="small">发货
          </el-button>
          <el-button v-else type="success" size="small" disabled>发货</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="订单状态" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" size="small" type="danger">未支付</el-tag>
          <el-tag v-else-if="scope.row.status === 2" size="small">付款成功</el-tag>
          <el-tag v-else-if="scope.row.status === 3" size="small">已发货</el-tag>
          <el-tag v-else-if="scope.row.status === 4" size="small">已收货</el-tag>
          <el-tag v-else-if="scope.row.status === 5" size="small" type="success">交易成功</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5,10,20]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog title="编辑" :visible.sync="dialogFormVisible">
      <el-form label-width="80px">
        <el-form-item label="用户">
          <el-input v-model="pojo.userId"></el-input>
        </el-form-item>
        <el-form-item label="用户备注">
          <el-input v-model="pojo.userMessage"></el-input>
        </el-form-item>
        <el-form-item label="地址id">
          <el-input v-model="pojo.addressId"></el-input>
        </el-form-item>

        <el-button type="primary" @click="handleSave()">保存</el-button>
        <el-button @click="dialogFormVisible = false">关闭</el-button>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import orderApi from '@/api/order'

  export default {
    data() {
      return {
        list: [],
        total: 0, // 总记录数
        currentPage: 1, // 当前页
        pageSize: 10, // 每页大小
        searchMap: {}, // 查询条件
        dialogFormVisible: false, // 编辑窗口是否可见
        pojo: {}, // 编辑表单绑定的实体对象
        id: '', // 当前用户修改的ID
        statusList: [
          {
            id: 1,
            name: '未支付'
          }, {
            id: 2,
            name: '付款成功'
          }, {
            id: 3,
            name: '已发货'
          }, {
            id: 4,
            name: '已收货'
          }, {
            id: 5,
            name: '交易成功'
          }
        ]
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        orderApi.search(this.currentPage, this.pageSize, this.searchMap).then(response => {
          console.log('page, size:' + this.currentPage + ', ' + this.pageSize)
          this.list = response.data.rows
          this.total = response.data.total
        })
      },
      handleSave() {
        orderApi.update(this.id, this.pojo).then(response => {
          this.$message({
            message: response.message,
            type: (response.flag ? 'success' : 'error')
          })
          if (response.flag) { // 如果成功
            this.fetchData() // 刷新列表
          }
        })
        this.dialogFormVisible = false // 关闭窗口
      },
      handleEdit(id) {
        this.id = id
        this.dialogFormVisible = true // 打开窗口
        if (id !== '') { // 修改
          orderApi.findById(id).then(response => {
            if (response.flag) {
              this.pojo = response.data
            }
          })
        } else {
          this.pojo = {} // 清空数据
        }
      },
      handleDelete(id) {
        this.$confirm('确定要删除此纪录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          orderApi.deleteById(id).then(response => {
            this.$message({ message: response.message, type: (response.flag ? 'success' : 'error') })
            if (response.flag) {
              this.fetchData() // 刷新数据
            }
          })
        })
      },
      totalPrice(orderItems) {
        var total = 0
        for (var i = 0; i < orderItems.length; i++) {
          var item = orderItems[i]
          total += item.number * item.book.price
        }
        return total
      },
      toSend(id) {
        orderApi.send(id).then(response => {
          this.fetchData()
          this.$message({
            message: '发货成功',
            type: 'success'
          })
        })
      },
      handleSizeChange(size) {
        this.pageSize = size
        this.fetchData()
      },
      handleCurrentChange(currentPage) {
        this.currentPage = currentPage
        this.fetchData()
      }
    }
  }
</script>
